package com.kc.sjwh.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.kc.sjwh.entity.bo.PdtthzBo;
import com.kc.sjwh.entity.bo.UserBo;
import com.kc.sjwh.entity.po.Pdtthz;
import com.kc.sjwh.other.AjaxRes;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.service.PdtthzService;

/**
 * pdt通话组action
 * @author zhangguihua(hua7381@163.com)
 * @date 2017年10月17日
 */
@Controller
@RequestMapping("pdtthz")
public class PdtthzAction extends BaseAction {
	static Logger logger = Logger.getLogger(PdtthzAction.class);
	
	@Autowired
	PdtthzService service;
	
	/**
	 * 打开列表页
	 * @param req
	 * @return
	 */
	@RequestMapping("list")
	public String list(HttpServletRequest req) {
		return "pdtthz/pdtthzList";
	}
	
	/**
	 * 分页查询
	 * @param req
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getPage")
	public String getPage(HttpServletRequest req, int pageSize, int pageNum) {
		Map<String, String> m = super.getParamMap(req);
		
		if(!SecurityUtils.getSubject().hasRole("admin")) {
			m.put("dwdm", getLoginUser().getSsdwdm());
		}
		
		Page<Pdtthz> page = service.getPage(pageSize, pageNum, m);
		
		StringBuffer sb = new StringBuffer();
		for(Pdtthz one : page.getList()) {//数据行
			PdtthzBo bo = (PdtthzBo)one;
			sb.append("<tr>");
			sb.append("<td>"+bo.getThzbh()+"</td>");
			sb.append("<td>"+bo.getThzmc()+"</td>");
			String dwmc = bo.getZzjgmc();
			if(null == dwmc || "".equals(dwmc)){
				dwmc = "-";
			}
			sb.append("<td>"+dwmc+"</td>");
			sb.append("<td>"+bo.getThzlx_()+"</td>");
			sb.append("<td>");
			sb.append("	<a href='form?id="+bo.getThzbh()+"'>编辑</a> &nbsp;");
			sb.append("	<a onclick=\"deletes('"+bo.getThzbh()+"')\">删除</a>");
			sb.append("</td>");
			sb.append("</tr>");
		}
		
		//返回结果
		Map data = new HashMap();
		data.put("total", page.getTotal());
		data.put("html", sb.toString());
		return JSON.toJSONString(new AjaxRes(data));
	}
	
	/**
	 * 打开表单(新增或修改)
	 * @param id
	 */
	@RequestMapping("form")
	public String form(HttpServletRequest req, String id) {
		req.setAttribute("act", id==null?"add":"update");
		req.setAttribute("actName", id==null?"新增":"修改");
		req.setAttribute("one", service.form(id));
		req.setAttribute("loginUser", super.getLoginUser());
		return "pdtthz/pdtthzForm";
	}
	
	/**
	 * 保存（新增或修改）
	 * @param one
	 * @param act add/update
	 */
	@ResponseBody
	@RequestMapping("save")
	public String save(Pdtthz one, String act) {
		if("add".equals(act)) {
			UserBo user = getLoginUser();
			one.setCjr(user.getMc());
			one.setZzjgdm(user.getSsdwdm());
			one.setZzjgmc(user.getSsdwmc());
			service.add(one);
		} else {
			one.setGxr(getLoginUser().getMc());
			service.update(one);
		}
		return JSON.toJSONString(new AjaxRes());
	}
	
	/**
	 * 删除
	 * @param req
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String delete(HttpServletRequest req, String id) {
		service.delete(id);
		return JSON.toJSONString(new AjaxRes());
	}
}
